package com.szqhyxj.youxiangju.biz.set;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.InstallUtil;
import com.szqhyxj.youxiangju.biz.core.TokenManager;
import com.szqhyxj.youxiangju.biz.login.LoginActivity;
import com.szqhyxj.youxiangju.biz.set.aboutus.AboutUsActivity;
import com.szqhyxj.youxiangju.biz.set.changepassword.ChangePassWordActivity;
import com.szqhyxj.youxiangju.biz.update.bean.UpdateInfo;
import com.szqhyxj.youxiangju.sys.components.ChoicePopView;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.view.dialog.DownloadDialog;
import com.szqhyxj.youxiangju.sys.components.view.popview.UpdatePopView;
import com.szqhyxj.youxiangju.sys.util.ApkUtil;
import com.szqhyxj.youxiangju.sys.util.DeviceUtils;
import com.szqhyxj.youxiangju.sys.util.PhoneManager;
import com.umeng.analytics.MobclickAgent;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.http.HEAD;

/**
 * Created by leung on 2018/10/31.
 */

public class SettingActivity extends BaseActivity implements NavigationBar.NavigationBarListener, SettingContract.View, UpdatePopView.onClickImmediatelyGo {

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.memory)
    TextView memory;

    SettingPresenter presenter;
    @BindView(R.id.version_number)
    TextView versionNumber;
    @BindView(R.id.out)
    TextView out;
    private DownloadDialog downloadDialog;

    public static void start(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, SettingActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        navigationBar.setNavigationBarListener(this);
        presenter = new SettingPresenter(this);
        presenter.init();
        presenter.setVersionNumber(PhoneManager.getLocalVersion(this));
        versionNumber.setText(PhoneManager.getLocalVersionName(this));
        try {
            memory.setText(DeviceUtils.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.changepassword, R.id.scavenging, R.id.upload, R.id.aboutus, R.id.out})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.changepassword:
                if (TokenManager.INSTANCE.getMToken()==null||TokenManager.INSTANCE.getMToken().equals("")){
                    Toast.makeText(this,"未登录",Toast.LENGTH_LONG).show();
                }else{
                    ChangePassWordActivity.start(this);
                }

                break;
            case R.id.scavenging:
                try {
                    DeviceUtils.clearAllCache(this);
                    memory.setText(DeviceUtils.getTotalCacheSize(this));
                    showToast("成功清除缓存");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.upload:
                presenter.verifyAPK();
                break;
            case R.id.aboutus:
                AboutUsActivity.start(this);
                break;
            case R.id.out:
                final ChoicePopView outPopView = new ChoicePopView(this, R.layout.popview_out);
                outPopView.show();
                outPopView.setOnClickListener(new ChoicePopView.OnClickListener() {
                    @Override
                    public void onClickConfirm(View v) {
                        presenter.out();
                        TokenManager.INSTANCE.clear();

                        MobclickAgent.onProfileSignOff();
                        outPopView.dismiss();
                    }
                });
                break;
        }
    }

    @Override
    public void navigationLeft() {
        finish();
    }

    @Override
    public void navigationRight() {
    }

    @Override
    public void showOut() {
        String token = TokenManager.INSTANCE.getMToken();
        LoginActivity.start(this);
    }

    @Override
    public void showOutFail() {
        Toast.makeText(this, "退出登录失败！", Toast.LENGTH_LONG).show();
    }

    @Override
    public void showToast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showUpdate(UpdateInfo updateInfo) {
        UpdatePopView popView = new UpdatePopView(this, updateInfo);
        popView.setOnClickImmediatelyGo(this);
        popView.show();
    }

    @Override
    public void showDownCompleted(final String appFilePath) {
        if (downloadDialog != null) {
            downloadDialog.dismissDialog();
            downloadDialog = null;
        }
        //安装app
//        ApkUtil.openFile(new File(appFilePath), this);
        InstallUtil mInstallUtil = new InstallUtil(this, appFilePath);
        mInstallUtil.install();
    }

    @Override
    public void showDownProgress(int progress) {
        if (downloadDialog != null) {
            downloadDialog.setUpDateProgress(100, progress);
        }
    }

    @Override
    public void showDownloadFail() {
        if(downloadDialog!=null){
            downloadDialog.dismissDialog();
        }
    }

    @Override
    public void enshowout() {
        out.setBackground(getResources().getDrawable(R.drawable.background_btn_grey04));
        out.setEnabled(false);
    }

    @Override
    public void showout() {
        out.setBackground(getResources().getDrawable(R.drawable.background_btn04));
        out.setEnabled(true);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onImmediatelyGo(String url) {
        downloadDialog = new DownloadDialog(this);
        downloadDialog.builder().setMsg("版本更新中...").show();
        presenter.downloadAPK(ApkUtil.getAppSystemPath(this), url);
    }
}
